package com.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dao.TagDAO;
import com.model.Tag;
@Repository
public class TagDAOImpl implements TagDAO {
	@Autowired
    private SessionFactory sessionFactory;

	public void addTag(Tag tag) {
		sessionFactory.getCurrentSession().save(tag);
	}

	public void update(Tag tag) {
		sessionFactory.getCurrentSession().update(tag);
	}
	
	public Tag findById(int id){
		Tag t = (Tag)sessionFactory.getCurrentSession().load(Tag.class, id);
		return t;
	}
	
	@SuppressWarnings("unchecked")
	public List<Tag> findByName(String name) {
		
		Session s = sessionFactory.getCurrentSession();
		String hql="from Tag as t where t.name=:name";
		Query query=s.createQuery(hql);
		query.setString("name", name);
        return query.list();

	}

	public List getHotTag(int topN) {
		// TODO Auto-generated method stub
		Session s = sessionFactory.getCurrentSession();
		String hql="from Tag as t order by (t.articleNum+t.questionNum) desc";
		Query query=s.createQuery(hql);
		query.setMaxResults(topN);
		return query.list();
		
	}
	
}
